package org.wishwork.api.stock.in.query;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.wishwork.api.ExtendBase;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

import java.util.List;

/**
 * @description: 采购入库查询
 * @author: WangYX
 * @create: 2022-04-11 17:24
 * @Version: 1.0.0
 **/
@EqualsAndHashCode
@ToString
@Getter
public class StockInQueryRequest extends ExtendBase {

    /**
     * 采购单号列表，与修改时间不能同时为空.采购单号最大不能超过30条
     */
    private List<String> poIds;

    /**
     * 采购单状态;不填写默认查询已出库状态
     * 状态(WaitConfirm 待入库;Confirmed 已入库;Cancelled 取消)
     */
    private List<String> status;

    protected StockInQueryRequest(Integer pageIndex, Integer pageSize, String modifiedBegin, String modifiedEnd,
                                  List<String> poIds, List<String> status) {
        super(pageIndex, pageSize, modifiedBegin, modifiedEnd);
        this.poIds = poIds;
        this.status = status;
    }

    @Override
    public Builder toBuilder() {
        return new Builder()
                .pageIndex(this.pageIndex)
                .pageSize(this.pageSize)
                .modifiedBegin(this.modifiedBegin)
                .modifiedEnd(this.modifiedEnd)
                .poIds(this.poIds)
                .status(this.status);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder extends ExtendBase.Builder<Builder, StockInQueryRequest> {

        private List<String> poIds;
        private List<String> status;

        @Override
        public Builder modifiedBegin(String modifiedBegin) {
            this.modifiedBegin = modifiedBegin;
            return this;
        }

        @Override
        public Builder modifiedEnd(String modifiedEnd) {
            this.modifiedEnd = modifiedEnd;
            return this;
        }

        @Override
        public Builder pageIndex(Integer pageIndex) {
            this.pageIndex = pageIndex;
            return this;
        }

        @Override
        public Builder pageSize(Integer pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public Builder poIds(List<String> poIds) {
            if (poIds != null && poIds.size() > 0) {
                if (poIds.size() > 30) {
                    throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "采购单号列表，与修改时间不能同时为空.采购单号最大不能超过30条!");
                }
            }
            this.poIds = poIds;
            return this;
        }

        public Builder status(List<String> status) {
            this.status = status;
            return this;
        }

        @Override
        public StockInQueryRequest build() {
            this.pageIndex = this.pageIndex == null ? 1 : this.pageIndex < 0 ? 1 : this.pageIndex;
            this.pageSize = this.pageSize == null ? 30 : this.pageSize < 0 ? 30 : this.pageSize;
            if (this.poIds == null || this.poIds.size() <= 0) {
                keyIsEmptyTimeHandle(7);
            }
            modifiedTimeHandle(7);
            return new StockInQueryRequest(this.pageIndex, this.pageSize, this.modifiedBegin, this.modifiedEnd,
                    this.poIds, this.status);
        }
    }


}

